package com.xiangyang.pos.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.xiangyang.pos.R;
import com.xiangyang.pos.activity.ScanActivity;
import com.xiangyang.pos.bean.GetOrderDataBean;
import com.xiangyang.pos.utils.SharedUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/12/1 0001.
 *
 * 订单出库适配器
 *
 * @author YeDalin
 *
 */

public class GetOrderAdapter extends RecyclerView.Adapter<GetOrderAdapter.ViewHolder>{


    private List<GetOrderDataBean.GetOrderData> list;
    private Context context;

    public GetOrderAdapter(List<GetOrderDataBean.GetOrderData> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_get_order,parent,false);
        ViewHolder holder=new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if(list.size()!=0){

            initView(holder,position);

            initClick(holder,position);
        }

    }




    @SuppressLint("ResourceAsColor")
    private void initView(ViewHolder holder, int position){
        holder.tvGoodsName.setText(list.get(position).getTitle()+"/"
                +list.get(position).getMeasurement()
                +list.get(position).getUnit());

        holder.tvPrice.setText("￥"+list.get(position).getPrice());
        holder.tvNumber.setText("x"+list.get(position).getNum());
        holder.etGoodsCode.setText(list.get(position).getScanCode());

        holder.etGoodsCode.setText(list.get(position).getScanCode());
        if(list.get(position).getCode().equals(list.get(position).getScanCode())){
            holder.etGoodsCode.setTextColor(R.color.ok);
            holder.etGoodsCode.setFocusable(false);
            holder.etGoodsCode.setFocusableInTouchMode(false);
            holder.tvSureCode.setText("成功");
            list.get(position).setOK(true);


            boolean isC=true;

            for (int i = 0; i < list.size(); i++) {
                if(!list.get(i).isOK()){
                    isC=false;
                }
            }
            if(isC){
                if(isGetOrder!=null){
                    isGetOrder.okGetOrder();
                }
            }

        }else if(!list.get(position).getScanCode().equals("")){
            holder.etGoodsCode.setTextColor(R.color.no);
            holder.tvSureCode.setText("失败");
        }

    }

    //点击事件
    private void initClick(ViewHolder holder, int position){
        holder.tvSureCode.setOnClickListener(getOnClickListener(holder,position));
        holder.tvGoodsScan.setOnClickListener(getOnClickListener(holder,position));
    }


    //点击事件出来
    private View.OnClickListener getOnClickListener(final ViewHolder holder, final int position){
        View.OnClickListener listener=new View.OnClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.tv_goods_scan:

                        SharedUtils.setIntPrefs(context,"position",position);
                        new IntentIntegrator((Activity) context)
                                .setOrientationLocked(false)
                                .setCaptureActivity(ScanActivity.class) // 设置自定义的activity是CustomActivity
                                .initiateScan(); // 初始化扫描

//                        Intent intent=new Intent(context, ScanActivity.class);
//                        ((Activity)context).startActivityForResult(intent,2000);

                        break;
                    case R.id.tv_sure_code:

                        if(holder.etGoodsCode.getText().toString().equals(list.get(position).getCode())){
                            holder.etGoodsCode.setTextColor(R.color.ok);
                            holder.etGoodsCode.setFocusable(false);
                            holder.etGoodsCode.setFocusableInTouchMode(false);
                            holder.tvSureCode.setText("成功");
                            list.get(position).setOK(true);

                            list.get(position).setScanCode(holder.etGoodsCode.getText().toString());
                            notifyDataSetChanged();


                            for (int i = 0; i < list.size(); i++) {
                                if(!list.get(i).isOK()){
                                    return;
                                }
                            }
                            if(isGetOrder!=null){
                                isGetOrder.okGetOrder();
                            }

                        }else {
                            holder.etGoodsCode.setTextColor(R.color.no);
                            holder.tvSureCode.setText("失败");
                        }

                        break;
                }
            }
        };
        return listener;
    }


    //接口显示出库按钮
    public interface IsGetOrder{
        void okGetOrder();
    }
    private IsGetOrder isGetOrder;
    public void setIsGetOrder(IsGetOrder isGetOrder) {
        this.isGetOrder = isGetOrder;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView tvGoodsName;
        private TextView tvPrice;
        private TextView tvNumber;
        private TextView tvGoodsScan;
        private EditText etGoodsCode;
        private TextView tvSureCode;

        public ViewHolder(View itemView) {
            super(itemView);

            tvGoodsName=itemView.findViewById(R.id.tv_goods_name);
            tvPrice=itemView.findViewById(R.id.tv_price);
            tvNumber=itemView.findViewById(R.id.tv_number);
            tvGoodsScan=itemView.findViewById(R.id.tv_goods_scan);
            tvSureCode=itemView.findViewById(R.id.tv_sure_code);
            etGoodsCode=itemView.findViewById(R.id.et_goods_code);

        }
    }



}
